<template>
	<view class="con pb200 posire">
		<view v-if="edit" class="posifi width100vw height100vh  zindex50">
			
		</view>
		<view class="bgfff pg30 ra15 mt20">
			<view class="flex">
				<view class=" wh10-40 bg0E74CF"></view>
				<view class="color0E74CF size28 ml10 bold">企业信息</view>
			</view>
			<view class="flex-a-i border-bF4F4F4 size26">
				<view class=" width170">企业名称</view>
				<!-- <input :focus="tfocus" confirm-type="done" @input="inputconfirm" @blur="inputconfirm" v-model="name" placeholder="请输入企业名称" class="flex1 pb30 pt30" placeholder-style="font-size:26rpx;color:#999;" /> -->
				<input :focus="tfocus" confirm-type="done" @input="inputconfirm"  v-model="name" placeholder="请输入企业名称" class="flex1 pb30 pt30" placeholder-style="font-size:26rpx;color:#999;" />
			</view>
			
			<picker @change="changeaddress" mode="region">
				<view class="flex-a-i border-bF4F4F4 ">
					<view class="size26 width170">所在地区</view>
					<view v-if="address.length" class="size26  flex1 pb30 pt30">
						{{address[0]}} {{address[1]}} {{address[2]}}
					</view>
					<view v-else class="size26 color999 flex1 pb30 pt30">请选择所在地区</view>
					<uni-icons type="right"></uni-icons>
				</view>
			</picker>
			<picker @change="pickerChange" range-key="name" :range="array">
				<view class="flex-a-i border-bF4F4F4 ">
					<view class="size26 width170">企业类型 </view>
					<view v-if="company_type" class="size26  flex1 pb30 pt30">
						{{company_name}}
					</view>
					<view v-else class="size26 color999 flex1 pb30 pt30">请选择企业类型</view>
					<uni-icons type="right"></uni-icons>
				</view>
			</picker>
			
			
			
			<picker v-if="address.length" @change="comChange" range-key="name" :range="comarray">
				<view  class="flex-a-i pb30 border-bF4F4F4">
					<view class="size26 width170 pt30">所属工商所 </view>
					
					<view v-if="institution_name" class="size26  flex1  pt30">{{institution_name?institution_name:'请选择所属工商所'}}</view>
					<view v-else class="size26 color999 flex1  pt30">{{institution_name?'':'请选择所属工商所'}}</view>
					
					<uni-icons class="pt30" type="right"></uni-icons>
				</view>
			</picker>
			
			<view v-else @click="shns" class="flex-a-i pb30 border-bF4F4F4">
				<view class="size26 width170 pt30">所属工商所 </view>
				<view class="size26 color999 flex1  pt30">{{institution_name?'':'请选择所属工商所'}}</view>
				<uni-icons class="pt30" type="right"></uni-icons>
			</view>
			
			<view class="flex-a-i  size26 pt30">
				<view class=" width170">企业联系方式</view>
				<input v-model="contact" placeholder="请输入联系方式" class="flex1  " placeholder-style="font-size:26rpx;color:#999;" />
			</view>
		</view>
		
		<view class="bgfff pg30 ra15 mt30 ">
			<view class="flex">
				<view class=" wh10-40 bg0E74CF"></view>
				<view class="color0E74CF size28 ml10 bold">营业执照 </view>
			</view>
			<image v-if="licenseurl" @click="upimg" mode="widthFix" class="image mt30" :src="licenseurl"></image>
			<image v-else @click="upimg" mode="widthFix" class="image mt30" src="/static/ying.png"></image>
			<view class="colorA9A9A9 size24 pl70 pr70 mt30">请确保证件内容文字清晰可见，证件本身无残缺， 否则将会审核失败;</view>
		</view>
	</view>
	<view @click="gocompany" class="posifi bottom20 width1000 zindex60">
		<view class="bg0E74CF colorfff ra20 con flex-ju-c height90">
			<!-- {{user.is_company==1?'修改':'确定'}} -->
			{{but}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				contact:"",
				but:'确定',
				tfocus:false,
				edit:false,
				institution_name:"",
				address:[],
				array:[],
				comarray:[],
				name:"",
				name1:"",
				company_type:"",
				gid:"",
				company_name:"",
				licenseurl:"",
				user:{}
			}
		},
		computed:{
			 
		},
		onLoad() {
			
			this.user=uni.getStorageSync('user')
			this.http('data_dictionary',{}).then(res=>{
				this.array = res.data.list
				if(this.user.is_company==1){
					this.edit=true
					this.but = '点击可修改'
					this.http('company/info').then(res=>{
						
						var res = res.data.info
						this.address=[res.province,res.area,res.city]
						this.name = res.name
						this.name1 = res.name
						this.company_type = res.company_type
						this.array.map(item=>{
							if(item.value == res.company_type){
								this.company_name=item.name
							}
						})
						this.licenseurl=res.license
						this.gid = res.institution_id
						this.contact = res.contact
						this.institution()
					})
				}
			})
			
		},
		methods: {
			inputconfirm(){
				if(this.name1 != this.name){
					if(this.address.length){
						this.institution_name =''
						this.gid=''
						// this.institution()
					}
				}
				
				
			},
			upimg(){
				this.aupimg().then(res=>{
					this.licenseurl=res[0]
				})
			},
			aupimg(){
				return new Promise((resolve, reject) => {
					uni.chooseImage({
						count: 1,
						success: (res) => {
							uni.showLoading({
								mask: true
							})
							
							var image = res.tempFilePaths
							var ff = []
							for (let i = 0; i < image.length; i++) {
								uni.uploadFile({
									url: this.apiurl + 'upload/image',
									filePath: image[i],
									name: 'file', 
									header: {
									  'Authori-zation':'Bearer '+( uni.getStorageSync("token") ? uni.getStorageSync("token") : '')
									},
									success: (uploadFileRes) => {
										let data = JSON.parse(uploadFileRes.data)
										if(data.status == 110002){
											uni.showToast({
												title:data.msg,
												icon:"none"
											})
											uni.hideLoading()
										}else{
											ff.push(JSON.parse(uploadFileRes.data).data.url)
											if(ff.length == image.length){
												resolve(ff)
												uni.hideLoading()
											}
										}
									},
									fail(e) {
										console.log(2333,e)
										uni.hideLoading()
										uni.showModal({
											content: "网络错误,稍后再试"
										})
									}
								})
							}
						},
					})
				})
			},
			shns(){
				uni.showToast({
					title:"请先选择地区",
					icon:"none"
				})
			},
			comChange(e){
				this.gid=this.comarray[e.detail.value].id
				this.institution_name = this.comarray[e.detail.value].name
			},
			pickerChange(e){
				
				this.company_type = this.array[e.detail.value].value
				this.company_name = this.array[e.detail.value].name
			},
			
			gocompany(){
				
				if(this.but=='点击可修改'){
					this.tfocus = true
					this.edit=false
					this.but='确定修改'
					return
				}
				
				if(!this.name){
					uni.showToast({
						title:"请输入企业名称",
						icon:"none"
					})
					return
				}
				if(this.address.length==0){
					uni.showToast({
						title:"请选择地区",
						icon:"none"
					})
					return
				}
				if(!this.company_type){
					uni.showToast({
						title:"请选择企业类型",
						icon:"none"
					})
					return
				}
				if(!this.gid){
					uni.showToast({
						title:"请选择工商所",
						icon:"none"
					})
					return
				}
				if(!this.contact){
					uni.showToast({
						title:"请输入企业联系",
						icon:"none"
					})
					return
				}
				if(!this.licenseurl){
					uni.showToast({
						title:"请上传执照",
						icon:"none"
					})
					return
				}
				
				this.http('company/register',{
					name:this.name,
					province:this.address[0],
					area:this.address[1],
					city:this.address[2],
					company_type:this.company_type,
					institution_id:this.gid,
					contact:this.contact,
					license:this.licenseurl,
				},'post').then(res=>{
					uni.showToast({
						title:"已完成",
						icon:"none"
					})
					setTimeout(()=>{
						uni.reLaunch({
							url:"/pages/user/index"
						})
					},800)
				})
			},
			institution(){
				if(this.address.length){
					if(this.address[1]=='北京市'||this.address[1]=='天津市'){
						this.address[1]='市辖区'
					}
					this.http('institution/index',{
						province:this.address[0],
						area:this.address[1],
						city:this.address[2],
						// name:this.name,
					}).then(res=>{
						this.comarray = res.data.list
						
						if(this.user.is_company==1){
							this.comarray.map(item=>{
								if(item.id==this.gid){
									this.institution_name=item.name
								}
							})
						}
						
						
					})
					
				}else{
					uni.showToast({
						title:"请先选择地区",
						icon:"none"
					})
				}
			},
			changeaddress(option){
				this.address=option.detail.value
				this.institution_name =''
				this.gid=''
				this.institution()
			}
		}
	}
</script>

<style>

</style>
